﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NeuralStock.DomainService.Interfaces;
using NeuralStock.DomainService;
using Microsoft.Practices.Unity;

namespace NeuralStock.Tests.Data
{
    [TestClass]
    public class CompanyQuotationServiceTest
    {
        [TestMethod]
        public void GetMinPriceForYear_Sanok2010_ShouldReturnValidValue()
        {
            ICompanyService companyService = Bootstrapper.GetUnityContainer().Resolve<ICompanyService>();
            ICompanyQuotationService companyQuotationService = Bootstrapper.GetUnityContainer().Resolve<ICompanyQuotationService>();

            var companyId = companyService.Get("SANOK").Id;

            var price = companyQuotationService.GetMinPriceForYear(companyId, 2010);
            Assert.AreEqual(9.60m, price.Value);
        }

        [TestMethod]
        public void GetFirstQuotationDate_Sanok_ShouldReturnValidValue()
        {
            ICompanyService companyService = Bootstrapper.GetUnityContainer().Resolve<ICompanyService>();
            ICompanyQuotationService companyQuotationService = Bootstrapper.GetUnityContainer().Resolve<ICompanyQuotationService>();

            var companyId = companyService.Get("SANOK").Id;

            var date = companyQuotationService.GetFirstQuotationDate(companyId);
            Assert.AreEqual(new DateTime(1997, 1, 16), date);
        }

        [TestMethod]
        public void GetMaxReturnForCompanyStartingAfterFirstQuarter_Sanok20082010_ShouldReturnValidValue()
        {
            ICompanyService companyService = Bootstrapper.GetUnityContainer().Resolve<ICompanyService>();
            ICompanyQuotationService companyQuotationService = Bootstrapper.GetUnityContainer().Resolve<ICompanyQuotationService>();

            var companyId = companyService.Get("SANOK").Id;

            var maxReturn = companyQuotationService.GetMaxReturnForCompanyStartingAfterFirstQuarter(companyId, 2008, 2010);
            Assert.AreEqual(0.07463m, Math.Round(maxReturn, 5));
        }

        [TestMethod]
        public void GetQuotationsForYears__Kopex19992003_ShouldReturnValidList()
        {
            ICompanyService companyService = Bootstrapper.GetUnityContainer().Resolve<ICompanyService>();
            ICompanyQuotationService companyQuotationService = Bootstrapper.GetUnityContainer().Resolve<ICompanyQuotationService>();

            var companyId = companyService.Get("KOPEX").Id;

            var numberOfQuotes = companyQuotationService.GetQuotationsForYears(companyId, 1999, 2003).Count();
            Assert.AreEqual(1249, numberOfQuotes);
        }

        [TestMethod]
        public void GetMaxReturnForCompany_Lotos20062008_ShouldReturnValidValue()
        {
            ICompanyService companyService = Bootstrapper.GetUnityContainer().Resolve<ICompanyService>();
            ICompanyQuotationService companyQuotationService = Bootstrapper.GetUnityContainer().Resolve<ICompanyQuotationService>();

            var companyId = companyService.Get("LOTOS").Id;

            var maxReturn = companyQuotationService.GetMaxReturnForCompany(companyId, 2006, 2008);
            Assert.AreEqual(0.34004m, Math.Round(maxReturn, 5));
        }

        [TestMethod]
        public void GetReturnAfterWholePeriodStartingAfterFirstQuarter_Lotos20062008_ShouldReturnValidValue()
        {
            ICompanyService companyService = Bootstrapper.GetUnityContainer().Resolve<ICompanyService>();
            ICompanyQuotationService companyQuotationService = Bootstrapper.GetUnityContainer().Resolve<ICompanyQuotationService>();

            var companyId = companyService.Get("LOTOS").Id;

            var maxReturn = companyQuotationService.GetReturnAfterWholePeriodStartingAfterFirstQuarter(companyId, 2006, 2008);
            Assert.AreEqual(-0.79217m, Math.Round(maxReturn, 5));
        }

        [TestMethod]
        public void GetDaysForAchievingReturnStartingAfterFirstQuarter_Lotos200710PercentIn2Years_ShouldReturnValidValue()
        {
            ICompanyService companyService = Bootstrapper.GetUnityContainer().Resolve<ICompanyService>();
            ICompanyQuotationService companyQuotationService = Bootstrapper.GetUnityContainer().Resolve<ICompanyQuotationService>();

            var companyId = companyService.Get("LOTOS").Id;

            var days = companyQuotationService.GetDaysForAchievingReturnStartingAfterFirstQuarter(companyId, 2007, 2009, 0.10m);
            Assert.AreEqual(21, days);
        }

        [TestMethod]
        public void GetQuotesStartingAfterFirstQuarter_KGHM20052010_ShouldReturnValidList()
        {
            ICompanyService companyService = Bootstrapper.GetUnityContainer().Resolve<ICompanyService>();
            ICompanyQuotationService companyQuotationService = Bootstrapper.GetUnityContainer().Resolve<ICompanyQuotationService>();

            var companyId = companyService.Get("KGHM").Id;

            var numberOfQuotes = companyQuotationService.GetQuotesStartingAfterFirstQuarter(companyId, 2005, 2010).Count();
            Assert.AreEqual(1425, numberOfQuotes);
        }
    }
}
